package com.zkzx.driver.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zkzx.common.driver.constants.DriverStatusEnum;
import com.zkzx.common.driver.model.DriverListVO;
import com.zkzx.common.driver.param.DriverFilterParam;
import com.zkzx.common.global.Global;
import com.zkzx.common.vo.PageResult;
import com.zkzx.common.vo.RequestFilterParams;
import com.zkzx.driver.service.DriverService;
import com.zkzx.util.json.JSONResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/24/10:40
 * @Description:
 */
@Slf4j
@Component
public class DriverJob {

    @Autowired
    private DriverService driverService;


    @XxlJob("driverStatusJobHandler")
    public void driverStatusJobHandler(){
        XxlJobHelper.log("执行司机信息状态查询更新");

        List<String> statusList = new ArrayList<>();
        //审核中
        statusList.add(DriverStatusEnum.APPROVE_ING.name());
        DriverFilterParam driverFilterParam = new DriverFilterParam();
        driverFilterParam.setStatusList(statusList);
        // 分页处理 每次查询100条
        RequestFilterParams params = new RequestFilterParams();
        params.setPage(Global.ONE);
        params.setPerPage(100);
        int totalNum = driverService.find(JSONResultUtil.object2JSON(driverFilterParam), JSONResultUtil.object2JSON(params)).getTotalNum();
        log.info("执行司机审核状态查询更新:{}", totalNum);
        // 当查询到没有数据
        if(totalNum <= Global.ZERO){
            return;
        }

        int num = (int) Math.ceil((float)totalNum/100);
        for (int i = 1; i <= num; i++) {
            params.setPage(i);
            PageResult<DriverListVO> result = driverService.find(JSONResultUtil.object2JSON(driverFilterParam), JSONResultUtil.object2JSON(params));
            result.getResult().forEach(vo -> driverService.updateToWh(vo));
        }


    }


    @XxlJob("driverContractJobHandler")
    public void driverContractJobHandler(){
        XxlJobHelper.log("执行司机合同签署状态查询更新");

        List<String> statusList = new ArrayList<>();
        //已有数据共享中，审核中
        statusList.add(DriverStatusEnum.CONTRACT_ING.name());
        DriverFilterParam driverFilterParam = new DriverFilterParam();
        driverFilterParam.setStatusList(statusList);
        // 分页处理 每次查询100条
        RequestFilterParams params = new RequestFilterParams();
        params.setPage(Global.ONE);
        params.setPerPage(100);
        int totalNum = driverService.find(JSONResultUtil.object2JSON(driverFilterParam), JSONResultUtil.object2JSON(params)).getTotalNum();
        log.info("执行司机合同签署状态查询更新:{}", totalNum);
        // 当查询到没有数据
        if(totalNum <= Global.ZERO){
            return;
        }

        int num = (int) Math.ceil((float)totalNum/100);
        for (int i = 1; i <= num; i++) {
            params.setPage(i);
            PageResult<DriverListVO> result = driverService.find(JSONResultUtil.object2JSON(driverFilterParam), JSONResultUtil.object2JSON(params));
            result.getResult().forEach(vo -> driverService.updateContractToWh(vo));
        }


    }


}
